package com.example.service.impl;




import com.example.dao.MemberDao;
import com.example.domain.pojo.Member;
import com.example.domain.utils.MD5Utils;
import com.example.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional


public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;


    //根据电话查询是否是会员
    @Override
    public Member findTelement(String telephone) {
        Member findmember = memberDao.findmember(telephone);
        return findmember;
    }

    //保存会员信息,注册会员
    @Override
   public void add(Member member) {
        //判断密码是否为空,表单提交调用,将密码转为加密的密码
        String password = member.getPassword();
        //用户注册,对密码进行加密
        if(password!=null){
            //用户提交设置密码,使用MD5,加密
            password= MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.addmember(member);
    }

    //查询全年每月注册的会员数量
    @Override
    public List<Integer> findMemberCount(List<String> list) {

        //存放会员数量
        List<Integer> countList=new ArrayList<>();

        //遍历list集合,查询数量
        for (String month : list) {
            System.out.println(month);
            String start=month+"-1";
            String end=month+"-31";
            Map<String,String> map=new HashMap<>();
            map.put("start",start);
            map.put("end",end);
           Long count= memberDao.findCountByMonth(map);
           countList.add(Math.toIntExact(count));
        }
        return countList;
    }

    @Override
    public Long findMemberByDate(String date) {
        //根据日期查询今天的注册人数
       Long count= memberDao.findCount(date);
       return count;
    }

    @Override
    public Long findAllCount() {
        Long count= memberDao.findAllCount();
        return count;
    }

    //一周内新增会员数量
    @Override
    public Long findcountWeek(Map<String, String> week) {
        Long count=  memberDao.findWeekCount(week);
        return count;
    }

    @Override
    public Long findMonth(Map<String, String> month) {
        Long count= memberDao.findCountMonth(month);
        return count;
    }


}
